//index.js
//获取应用实例
const app = getApp();
const host = require('../../config');
console.log(app.globalData.unionid)
Page({
  data: {
    resObj:{},
    id:null,
    token:null
  },
  onLoad: function (options) {
      this.setData({
         id:options.id,
         token:options.token
      });
     this.getDetail(options.token,options.id);
  },
  onShow: function () {
     
  },
  GetQueryString:function(str,name){
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var index=str.indexOf("?");
     var search=str.slice(index)
     var r = search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
  },
  getDetail(token,id){
      var that=this;
      wx.request({
        url: host+"/trade/order/detail",
        data: {
          token:token,
          id:id
        },
        success: function(res) {
          if(res.data.error==0){
              that.setData({
                 resObj:res.data.order
              });
              console.log(res.data.order)
          }
        

        },
        fail: function(res) {
          
        }
      });
  },
  saveOrder(){
      var that=this;
      var id=this.data.id;
      wx.request({
        url: host+"/trade/pay/charge",
        data: {
          ids: [id],
          token: that.data.token
          
        },
        success: function(res) {
          var result=res.data;
          console.log(result)
          if(res.data.error==0){
            let timestamp = result.charge.timeStamp+'';
            let nonceStr = result.charge.nonceStr;
            let package1 = result.charge.package;
            let signType = result.charge.signType;
            let paySign = result.charge.paySign;
            wx.requestPayment({
               timeStamp: timestamp,
               nonceStr: nonceStr,
               package: package1,
               signType: signType,
               paySign: paySign,
              success:function(res){
                  wx.navigateBack({
                    delta: 1
                  });
                  console.log(res)
               },
                fail:function(res){
                   console.log(res)
                 
               }
            });
               
          }
        

        },
        fail: function(res) {
          
        }
      });

  }
  
})
